package com.zh.note.huawei.twohundred.WeAreTeam;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

/**
 * https://dream.blog.csdn.net/article/details/131958583
 * 难点：数据存储
 * <p>
 *
 * List<Set<Integer>> relationshipSets = new ArrayList<>();
 * List<int[]> verifyRelationships = new ArrayList<>();
 * 5 7
 * 1 2 0
 * 4 5 0
 * 2 3 0
 * 1 2 1
 * 2 3 1
 * 4 5 1
 * 1 5 1
 */
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        int m = Integer.parseInt(st.nextToken());

        // 存储同一队的信息
        List<Set<Integer>> relationshipSets = new ArrayList<>();
        // 存储待验证的信息
        List<int[]> verifyRelationships = new ArrayList<>();

        for (int i = 0; i < m; i++) {
            String[] relationship = br.readLine().trim().split(" ");
            int x = Integer.parseInt(relationship[0]);
            int y = Integer.parseInt(relationship[1]);
            int relation = Integer.parseInt(relationship[2]);

            if (relation != 0) { // 是待验证的
                verifyRelationships.add(new int[]{x, y, relation});
            } else {// c==0
                // 是同一组的
                boolean setAdded = false;
                for (int j = 0; j < relationshipSets.size(); j++) {
                    Set<Integer> set = relationshipSets.get(j);
                    if (set.contains(x) || set.contains(y)) { // 判断x,y是否已经存储过
                        set.add(x);
                        set.add(y);
                        setAdded = true;
                        break;
                    }
                }
                if (!setAdded) { // 没有存储过该元素，表示不是之前一组的，需要创建新的一组
                    Set<Integer> newSet = new HashSet<>();
                    newSet.add(x);
                    newSet.add(y);
                    relationshipSets.add(newSet);
                }
            }
        }
        // 验证是否一组
        for (int i = 0; i < verifyRelationships.size(); i++) {
            int[] relationship = verifyRelationships.get(i);
            int x = relationship[0];
            int y = relationship[1];
            int relation = relationship[2];

            if (relation != 1) {
                System.out.println("da pian zi");
                continue;
            }
            // 遍历relationshipSets，判断x,y是否同一组
            boolean isTeam = false;
            for (int j = 0; j < relationshipSets.size(); j++) {
                Set<Integer> set = relationshipSets.get(j);
                if (set.contains(x) && set.contains(y)) {
                    System.out.println("we are a team");
                    isTeam = true;
                    break;
                }
            }
            if (!isTeam) {
                System.out.println("we are not a team");
            }
        }
    }
}

